//
//  personChangePhoneViewController.m
//  shopping
//
//  Created by 马田成 on 2017/9/28.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "personChangePhoneViewController.h"

@interface personChangePhoneViewController ()
@property (weak, nonatomic) IBOutlet UILabel *yuanPhone;
@property (weak, nonatomic) IBOutlet UITextField *yuanPhoneTF;
@property (weak, nonatomic) IBOutlet UILabel *xinPhone;
@property (weak, nonatomic) IBOutlet UITextField *xinPhoneTF;

@property (weak, nonatomic) IBOutlet UITextField *ycYZMTF;
@property (weak, nonatomic) IBOutlet UIButton *shoujiYZM;


@end

@implementation personChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"绑定手机号"];
    
    
    
//    if ([self.type isEqualToString:@"1"]) {
//        [self setTitle:@"修改手机号"];
//        self.shoujiYZM.hidden = YES;
//        
//        
//    }else {
//    
//        self.yuanPhone.text = @"手机号";
//        self.xinPhone.text = @"验证码";
//        self.yanZhangM.hidden = YES;
//        self.faSongYanZMyc.hidden = YES;
//        self.viewYC.hidden = YES;
//        self.ycYZMTF.hidden = YES;
//        
//        
//        [self setTitle:@"绑定手机号"];
//    }
    
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)yzmClick:(UIButton *)sender {
    
    

    NSMutableDictionary *productParamsss=[[NSMutableDictionary alloc]init];
    //    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    
    
    [productParamsss setValue:self.yuanPhoneTF.text forKey:@"phone"];
    
    [[MNetworkHandler sharedInstance]conURL:adminLoginSendPhone
                                networkType:NetWorkPOST
                                     params:productParamsss
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([returnData[@"code"] intValue] == 1) {
                                       
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                       __block int timeout=60; //倒计时时间
                                       dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                                       dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
                                       dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
                                       
                                       dispatch_source_set_event_handler(_timer, ^{
                                           
                                           if(timeout<=0){ //倒计时结束，关闭
                                               
                                               dispatch_source_cancel(_timer);
                                               dispatch_async(dispatch_get_main_queue(), ^{
                                                   //设置界面的按钮显示 根据自己需求设置
                                                   [self.shoujiYZM setTitle:@"发送验证码" forState:UIControlStateNormal];
                                                   
                                                   self.shoujiYZM.userInteractionEnabled = YES;
                                               });
                                           }else{
                                               int seconds = timeout % 61;
                                               
                                               NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                                               dispatch_async(dispatch_get_main_queue(), ^{
                                                   //设置界面的按钮显示 根据自己需求设置
                                                   //NSLog(@"____%@",strTime);
                                                   [UIView beginAnimations:nil context:nil];
                                                   [UIView setAnimationDuration:1];
                                                   [self.shoujiYZM setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                                                   [UIView commitAnimations];
                                                   self.shoujiYZM.userInteractionEnabled = NO;
                                               });
                                               timeout--;
                                           }
                                       });
                                       dispatch_resume(_timer);
                                   }else{
                                       
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                                   
                               }
                               failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                //   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];

    
    
    
}
- (IBAction)sureBtnClick:(UIButton *)sender {
    
    
//    if ([self.type isEqualToString:@"1"]) {
//        NSMutableDictionary *productParamsss=[[NSMutableDictionary alloc]init];
//        NSString *token = [PublicFunction getConfigWithKey:@"token"];
//        
//        
//        [productParamsss setValue:self.xinPhoneTF.text forKey:@"newphone"];
//        [productParamsss setValue:token forKey:@"token"];
//        [productParamsss setValue:self.yuanPhoneTF.text forKey:@"oldphone"];
//        [productParamsss setValue:self.ycYZMTF.text forKey:@"code"];
//        [[MNetworkHandler sharedInstance]conURL:adminLxlpersonEditPhone networkType:NetWorkPOST params:productParamsss delegate:nil showHUD:NO successBlock:^(id returnData) {
//            
//            
//            if ([[returnData objectForKey:@"code"]intValue]==1) {
//                
//                [self.navigationController popViewControllerAnimated:YES];
//                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
//                
//                
//            }
//            
//            else {
//                
//                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
//                
//            }
//            
//        } failureBlock:^(NSError *error) {
//            
//            NSLog(@"%@",error);
//            
////            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
//            
//        }];
//
//    }else {
//    
//    
//        NSMutableDictionary *productParamsss=[[NSMutableDictionary alloc]init];
//       
//        NSString *token = [PublicFunction getConfigWithKey:@"token"];
//        
//        [productParamsss setValue:token forKey:@"token"];
//        [productParamsss setValue:self.yuanPhoneTF.text forKey:@"phone"];
//        [productParamsss setValue:self.xinPhone.text forKey:@"code"];
//        [[MNetworkHandler sharedInstance]conURL:adminLxlpersonBindphone networkType:NetWorkPOST params:productParamsss delegate:nil showHUD:NO successBlock:^(id returnData) {
//            
//            
//            if ([[returnData objectForKey:@"code"]intValue]==1) {
//                [self.navigationController popViewControllerAnimated:YES];
//                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
//                
//                
//            }
//            
//            else {
//                
//                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
//                
//            }
//            
//        } failureBlock:^(NSError *error) {
//            
//            NSLog(@"%@",error);
//            
////            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
//            
//        }];
//
//    }
    
    
    
    
    
    NSMutableDictionary *productParamsss=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    
    [productParamsss setValue:token forKey:@"token"];
    [productParamsss setValue:self.yuanPhoneTF.text forKey:@"phone"];
    [productParamsss setValue:self.xinPhone.text forKey:@"code"];
    [[MNetworkHandler sharedInstance]conURL:adminLxlpersonBindphone networkType:NetWorkPOST params:productParamsss delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
            
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        //            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
